package com.example.dataimport.configurer;

import com.example.dataimport.dao.ExcelImportDao;
import com.example.dataimport.jobs.ExcelImport;
import com.example.dataimport.model.ColumnMappingBO;
import com.example.dataimport.model.ExcelImportJobBO;
import com.example.dataimport.model.TableMappingBO;
import com.example.dataimport.service.ColumnMappingService;
import com.example.dataimport.util.GlobalConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling
public class CompleteScheduleConfig implements SchedulingConfigurer {
    @Autowired
    ColumnMappingService columnMappingService;

    @Autowired
    ExcelImportDao excelImportDao;

    private Map<String, ExcelImportJobBO> folderPathToExcelImportJob = new HashMap<>();

    private List<ExcelImport> excelImportList = new ArrayList<>();

    private void addJob(TableMappingBO tableMappingBO, Map<String,String> p_colMap){
        String folderPath = tableMappingBO.getExcel_path();
        String tableName = tableMappingBO.getTable_name();
        ExcelImportJobBO p_job;
        Map<String, Map<String, String>> tableNameToColMap;

        if(folderPathToExcelImportJob.containsKey(folderPath)){
            p_job = folderPathToExcelImportJob.get(folderPath);
            tableNameToColMap = p_job.getTableNameToColMap();
            if(!tableNameToColMap.containsKey(tableName)){
                tableNameToColMap.put(tableName, p_colMap);
            }
            p_job.setTableNameToColMap(tableNameToColMap);
        }
        else{
            p_job = new ExcelImportJobBO();
            p_job.setBak_path(tableMappingBO.getBak_path());
            p_job.setExcel_path(folderPath);
            p_job.setName(tableMappingBO.getName());
            tableNameToColMap = new HashMap<>();
            tableNameToColMap.put(tableName, p_colMap);
            p_job.setTableNameToColMap(tableNameToColMap);
        }
        folderPathToExcelImportJob.put(folderPath, p_job);
    }

    public void createExcelImportJobs(){
        List<TableMappingBO> tableMappingBOList = columnMappingService.getAllTables();

        for(int count = 0; count < tableMappingBOList.size(); count ++){
            Map<String, String> colMap = new HashMap<>();
            TableMappingBO tableMappingBO = tableMappingBOList.get(count);
            List<ColumnMappingBO> columnMappingBOList = columnMappingService.getColumnMappingByTableID(tableMappingBO.getID());
            for (ColumnMappingBO columnMappingBO: columnMappingBOList) {
                colMap.put(columnMappingBO.getExcel_column(), columnMappingBO.getTable_column());
            }
            addJob(tableMappingBO, colMap);
        }

        for(Map.Entry<String, ExcelImportJobBO> entry:folderPathToExcelImportJob.entrySet()){
            String folderPath = entry.getKey();
            ExcelImportJobBO excelImportJobBO = entry.getValue();
            ExcelImport excelImport = new ExcelImport(excelImportJobBO);
            excelImport.setFolderPath(folderPath);
            excelImport.setExcelImportDao(excelImportDao);
            excelImportList.add(excelImport);
        }
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        createExcelImportJobs();
        excelImportList.forEach(
                excelImport -> {
                    Runnable runnable = () -> {
                        excelImport.folderScan();
                    };
                    //任务触发器
                    Trigger trigger = triggerContext -> {
                        //获取定时触发器，这里可以每次从数据库获取最新记录，更新触发器，实现定时间隔的动态调整
                        CronTrigger cronTrigger = new CronTrigger(GlobalConst.cron);
                        return cronTrigger.nextExecutionTime(triggerContext);
                    };

                    //注册任务
                    taskRegistrar.addTriggerTask(runnable, trigger);
                }

        );
    }

}
